<template>
  <div class="home">
    <el-card class="home-soup">
      <span>新增励志文案</span>
      <div class="soup-add">
        <el-row :gutter="16">
          <el-col :span="22">
            <el-input
              placeholder="请输入励志文案"
              v-model="soupText"
            ></el-input>
          </el-col>
          <el-col :span="2">
            <el-button
              :disabled="soupText ? false : true"
              type="primary"
              @click="addSoup"
              >新增</el-button
            >
          </el-col>
        </el-row>
      </div>
    </el-card>

    <!-- 图表区域 -->
    <el-card class="home-figure">
      <span>系统分析</span>
      <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
      <div
        v-loading="loading"
        id="main"
        style="width: 840px; height: 400px"
      ></div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import * as echarts from 'echarts';
import { ElMessage } from 'element-plus';
import { ref, getCurrentInstance, ComponentInternalInstance, onMounted } from 'vue';

const { appContext } = getCurrentInstance() as ComponentInternalInstance;
const proxy = appContext.config.globalProperties;

// 鸡汤内容
const soupText = ref('');
// 是否在加载中
const loading = ref(true);
// 今日签到总人数
const clockPeopleNumber = ref(0);
// 帖子总数
const postTotal = ref(0);
// 总人数
const peopleTotal = ref(0);
// 鸡汤总数
const chickenCount = ref(0);
// 标签总数
const tagTotal = ref(0);

onMounted(() => {
  initEcharts();
})

// 新增鸡汤
function addSoup() {
  if (!soupText.value) {
    return ElMessage({
      message: '请先输入新增鸡汤',
      type: 'warning',
      duration: 1000,
    });
  }
  proxy.$request.manager
    .addSoup({
      soupContent: soupText.value,
    })
    .then((res: any) => {
      ElMessage({
        message: res.msg,
        type: 'success',
        duration: 1000,
      });
      initEcharts();
      soupText.value = '';
    });
};
// 获取用户当日学习分析echarts图表信息
function initEcharts() {
  loading.value = true;
  var chartDom = document.getElementById('main');
  var myChart = echarts.init(chartDom);
  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption({
    title: {
      text: '系统总体情况',
      subtext: '总体数据',
      left: 'center',
    },
    tooltip: {
      trigger: 'item',
    },
    legend: {
      orient: 'vertical',
      left: 'left',
    },
    series: [
      {
        type: 'pie',
        radius: '50%',
        data: [],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
    ],
  });

  // 获取今天所有的签到人数
  proxy.$request.userSign.sameday().then((res1: any) => {
    clockPeopleNumber.value = res1.data;
    proxy.$request.discussPost
      .allPosts({ pageNum: 1, pageSize: 1 })
      .then((res2: any) => {
        postTotal.value = res2.data.total;
        proxy.$request.manager
          .getAllUser({ currentPage: 1, pageSize: 1 })
          .then((res: any) => {
            peopleTotal.value = res.data.total;
            proxy.$request.manager.getChickenCount().then((res: any) => {
              chickenCount.value = res.data;
              proxy.$request.tag.getAllTag().then((res: any) => {
                var tagSum = 0;
                res.data.forEach((item: any) => {
                  tagSum += item.tagList.length;
                });
                tagTotal.value = tagSum;
                myChart.setOption({
                  series: [
                    {
                      data: [
                        {
                          name: '今日签到总人数',
                          value: clockPeopleNumber.value,
                        },
                        { name: '发布帖子总数', value: postTotal.value },
                        { name: '系统注册总人数', value: peopleTotal.value },
                        { name: '鸡汤总数', value: chickenCount.value },
                        { name: '标签总数', value: tagTotal.value },
                      ],
                    },
                  ],
                });
                loading.value = false;
              });
            });
          });
      });
  });
};
</script>

<style lang="less" scoped>
.home-soup,
.home-figure {
  text-align: left;
  span {
    display: block;
    font-size: 18px;
    font-weight: 700;
    margin-bottom: 10px;
  }
}

.home-soup {
  margin: 20px;

  .soup-add {
    margin: 20px 0;
  }
}

.home-figure {
  margin: 0 20px;
}
</style>
